package com.yang;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * ClassName: TestPing
 * Description:
 * Date: 2021/3/19 10:14
 * Company: TravelSky
 *
 * @author YangXin
 */
public class TestPing {

    public static void main(String[] args) {
        //连接远程redis服务
        Jedis jedis = new Jedis("39.105.46.187",6379);
        System.out.println(jedis.ping());
        jedis.flushDB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("yang","xin");
        //开启事务
        Transaction multi = jedis.multi();
        String result = jsonObject.toJSONString();
        //开启乐观锁
        jedis.watch(result);
        try {
            multi.set("user1",result);
            multi.set("user2",result);
            //执行事务
            multi.exec();
        } catch (Exception e) {
            //放弃事务
            multi.discard();
            e.printStackTrace();
        }finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }
    }
}
